<%@page import="java.util.Calendar"%>
<%@page import="com.glowdayz.common.PageHandler"%>
<%@page import="com.glowdayz.common.MultipartBox.FileInfo"%>
<%@page import="com.glowdayz.glowmee.dao.PouchDAO"%>
<%@page import="com.glowdayz.glowmee.cmd.PouchCMD"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.glowdayz.common.DateTime"%>
<%@page import="com.glowdayz.common.ApiHandler"%>
<%@page import="com.glowdayz.common.DBHandler"%>
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%
	DBHandler.getInstance().connect();
	
	ApiHandler web = new ApiHandler(request, out, "JSPglowmee");
	
	int nowPage = web.getInt("page");
	int count = web.getInt("count");
	String pouchCategory = web.getValue("pouchCategory");
	
	int order = web.getInt("order");
	int gender = web.getInt("gender");
	int age = web.getInt("age");
	int skin = web.getInt("skin");
	int id = web.getInt("id");
	int wish = web.getInt("wish");
	
	int addIdPouch = web.getInt("addIdPouch");
	
	int idBrand = web.getInt("idBrand");
	
	String orderValue = null;
	if (order == 1) {
		orderValue = "p.pouchScore DESC, p.readCount DESC, p.create_date DESC";
	} else if (order == 2) {
		orderValue = "count2 DESC, p.create_date DESC";
	} else if (order == 3) {
		orderValue = "p.recommendCount DESC, p.create_date DESC";
	} else if (order == 4) {
		orderValue = "p.readCount DESC, p.create_date DESC";
	}
	
	String whereValue = "";
	
	if (idBrand > 0) {
		whereValue += " AND b.isDisplay=1 AND b.idBrand=" + idBrand;
	}
	
	if (addIdPouch > 0) {
		whereValue += " AND p.idPouch=" + addIdPouch;
	}
	
	if (id > 0) {
		whereValue += " AND r.idRegister=" + id;
	}
	
	if (wish > 0) {
		whereValue += " AND wm.idRegister=" + wish;
	}
	
	if (gender == 1 || gender == 2) {
		if (gender == 1) {
			gender = 7;
		} else if (gender == 2) {
			gender = 6;
		}
		whereValue += " AND r.gender=" + gender;
	}
	
	if (age > 0) {
		Calendar cal = Calendar.getInstance();
		int now = cal.get(Calendar.YEAR);
		
		switch (age) {
		case 10:
			whereValue += " AND (" + now + "-r.birthYear) > 8 AND (" + now + "-r.birthYear) <= 18";
			break;
		case 20:
			whereValue += " AND (" + now + "-r.birthYear) > 18 AND (" + now + "-r.birthYear) <= 28";
			break;
		case 30:
			whereValue += " AND (" + now + "-r.birthYear) > 28 AND (" + now + "-r.birthYear) <= 38";
			break;
		case 40:
			whereValue += " AND (" + now + "-r.birthYear) > 38";
			break;
		}
	}
	
	if (skin > 0) {
		String skinType = null;
		switch (skin) {
		case 1:
			skinType = "건성";
			break;
		case 2:
			skinType = "지성";
			break;
		case 3:
			skinType = "중성";
			break;
		case 4:
			skinType = "민감성";
			break;
		case 5:
			skinType = "복합성";
			break;
		}
		whereValue += " AND c.commonText='" + skinType + "'";
	}
	
	ArrayList<PouchCMD> list = null;
	if (pouchCategory.equals("")) {
		list = PouchDAO.getInstance().getPickListXml(nowPage, count, orderValue, whereValue);
	} else if (pouchCategory.equals("추천픽")) {
		list = PouchDAO.getInstance().getPickListXml(nowPage, count, orderValue, " AND p.editerPick=1" + whereValue);
	} else if (pouchCategory.equals("에디터픽")) {
		list = PouchDAO.getInstance().getPickListXml(nowPage, count, orderValue, " AND r.userType='ADMIN'" + whereValue);
	} else {
		whereValue += " AND r.userType='USER'";
		list = PouchDAO.getInstance().getPickListXml(nowPage, count, orderValue, " AND pc.pouchCategoryText='" + pouchCategory + "'" + whereValue);
	}
	
	if (list == null) {
		DBHandler.getInstance().disconnect();
		web.result(null, "데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.");
		return;
	}
	
	PageHandler p = PageHandler.getInstance();
	DBHandler.getInstance().disconnect();
%>
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
	<channel>
		<!-- ##### 이 데이터를 소개하는 부분 (필수항목들) ##### -->
		<!-- 제목 -->
		<title>글로우미</title>
		<!-- XML 생성일시 -->
		<pubDate><%=DateTime.getInstance().getNowDateTimeString()%></pubDate>
		
		<!-- ##### 추가적인 정보들 (일반적으로 페이징 처리에 관련된 변수들) ##### -->
		<!-- 태그이름은 멤버변수이름, 값은 getter 호출 -->
		<nowPage><%=p.getNowPage()%></nowPage>
		<totalArticleCount><%=p.getTotalArticleCount()%></totalArticleCount>
		<pageArticleCount><%=p.getPageArticleCount()%></pageArticleCount>
		<pageCount><%=p.getPageCount()%></pageCount>
		<pageGroupCount><%=p.getPageGroupCount()%></pageGroupCount>
		<startPage><%=p.getStartPage()%></startPage>
		<endPage><%=p.getEndPage()%></endPage>
		<startRec><%=p.getStartRec()%></startRec>
		<lastGroupFirstPage><%=p.getLastGroupFirstPage()%></lastGroupFirstPage>
		
		<!-- ##### 게시물 정보 반복 처리 ##### -->
		<!-- 추가적인 정보를 덧붙이거나, 필요 없는 정보는 삭제 가능함 -->
		<item>
			<!-- 파우치 항번 -->
			<idPouch><![CDATA[1]]></idPouch>
			<!-- 파우치 카테고리 -->
			<idPouchCategoryValue><![CDATA[아무거나]]></idPouchCategoryValue>
			<!-- 브랜드 항번 -->
			<idBrand><![CDATA[0]]></idBrand>
			<!-- 브랜드 이름 -->
			<idBrandValue><![CDATA[null]]></idBrandValue>
			<!-- 파우치 이름 -->
			<pouchTitle><![CDATA[아무거나 파우치]]></pouchTitle>
			<!-- 파우치 내용 -->
			<pouchText><![CDATA[아무거나 파우치입니다.]]></pouchText>
			<!-- 에디터의픽 유무 -->
			<editerPick><![CDATA[0]]></editerPick>
			<!-- 생성일시 -->
			<create_date><![CDATA[20140101010101]]></create_date>
			<!-- 회원 항번 -->
			<idRegister><![CDATA[0]]></idRegister>
			<!-- 닉네임 -->
			<idRegisterValue><![CDATA[null]]></idRegisterValue>
			<!-- 회원종류 -->
			<userType><![CDATA[null]]></userType>
			<!-- 생년월일 -->
			<birthYear><![CDATA[0]]></birthYear>
			<!-- 피부타입 -->
			<skinType>null</skinType>
			<!-- 제품수 -->
			<productCount><![CDATA[0]]></productCount>
			<!-- 추천수 -->
			<recommendCount><![CDATA[0]]></recommendCount>
			<!-- 덧글수 -->
			<commentCount><![CDATA[0]]></commentCount>
			<!-- 제품 이미지 -->
			<image>null</image>
			<thumbnail>null</thumbnail>
			<!-- 유저 이미지 -->
			<user_image>null</user_image>
			<user_thumbnail>null</user_thumbnail>
			<!-- 브랜드 이미지 -->
			<brand_image>null</brand_image>
			<brand_thumbnail>null</brand_thumbnail>
		</item>
		<%
			if (list != null) {
				for (int i=0; i<list.size(); i++) {
					PouchCMD item = list.get(i);
					FileInfo file = item.getImage();
					FileInfo userFile = item.getUserImage();
					FileInfo brandFile = item.getBrandImage();
		%>
		<item>
			<!-- 파우치 항번 -->
			<idPouch><![CDATA[<%=item.getIdPouch()%>]]></idPouch>
			<!-- 파우치 카테고리 -->
			<idPouchCategoryValue><![CDATA[<%=item.getIdPouchCategoryValue()%>]]></idPouchCategoryValue>
			<!-- 브랜드 항번 -->
			<idBrand><![CDATA[<%=item.getIdBrand()%>]]></idBrand>
			<!-- 브랜드 이름 -->
			<idBrandValue><![CDATA[<%=item.getIdBrandValue()%>]]></idBrandValue>
			<!-- 파우치 이름 -->
			<pouchTitle><![CDATA[<%=item.getPouchTitle()%>]]></pouchTitle>
			<!-- 파우치 내용 -->
			<pouchText><![CDATA[<%=item.getPouchText()%>]]></pouchText>
			<!-- 에디터의픽 유무 -->
			<editerPick><![CDATA[<%=item.getEditerPick()%>]]></editerPick>
			<!-- 생성일시 -->
			<create_date><![CDATA[<%=item.getCreate_date()%>]]></create_date>
			<!-- 회원 항번 -->
			<idRegister><![CDATA[<%=item.getIdRegister()%>]]></idRegister>
			<!-- 닉네임 -->
			<idRegisterValue><![CDATA[<%=item.getIdRegisterValue()%>]]></idRegisterValue>
			<!-- 회원종류 -->
			<userType><![CDATA[<%=item.getUserType()%>]]></userType>
			<!-- 생년월일 -->
			<birthYear><![CDATA[<%=item.getBirthYear()%>]]></birthYear>
			<% if (item.getSkinType() != null) {%>
			<!-- 피부타입 -->
			<skinType><![CDATA[<%=item.getSkinType()%>]]></skinType>
			<% } else { %>
			<!-- 피부타입 -->
			<skinType>null</skinType>
			<% } %>
			<!-- 제품수 -->
			<productCount><![CDATA[<%=item.getProductCount()%>]]></productCount>
			<!-- 추천수 -->
			<recommendCount><![CDATA[<%=item.getRecommendCount()%>]]></recommendCount>
			<!-- 덧글수 -->
			<commentCount><![CDATA[<%=item.getCommentCount()%>]]></commentCount>
			<% if (file != null) {%>
			<!-- 제품 이미지 -->
			<image><![CDATA[/glowmee/download.jsp?<%=file.toString("utf-8")%>]]></image>
			<thumbnail><![CDATA[/glowmee/download.jsp?<%=file.toString("utf-8")%>&size=320]]></thumbnail>
			<% } else { %>
			<image>null</image>
			<thumbnail>null</thumbnail>
			<% } %>
			<% if (userFile != null) {%>
			<!-- 유저 이미지 -->
			<user_image><![CDATA[/glowmee/download.jsp?<%=userFile.toString("utf-8")%>]]></user_image>
			<user_thumbnail><![CDATA[/glowmee/download.jsp?<%=userFile.toString("utf-8")%>&size=160]]></user_thumbnail>
			<% } else { %>
			<user_image>null</user_image>
			<user_thumbnail>null</user_thumbnail>
			<% } %>
			<% if (brandFile != null) {%>
			<!-- 브랜드 이미지 -->
			<brand_image><![CDATA[/glowmee/download.jsp?<%=brandFile.toString("utf-8")%>]]></brand_image>
			<brand_thumbnail><![CDATA[/glowmee/download.jsp?<%=brandFile.toString("utf-8")%>&size=160]]></brand_thumbnail>
			<% } else { %>
			<brand_image>null</brand_image>
			<brand_thumbnail>null</brand_thumbnail>
			<% } %>
		</item>
		<%
				}
			}
		%>
	</channel>
</rss>